﻿package com.oxylusflash.AccordionMenu
{
	
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.text.TextField;
	import fl.data.DataProvider;
	import caurina.transitions.Tweener;
	import caurina.transitions.properties.ColorShortcuts;
	ColorShortcuts.init();
	
	public class MenuItem extends Sprite
	{
		public var outState:Sprite;
		public var overState:Sprite;
		public var hitState:Sprite;
		public var Title:TextField;
		public var subTitle:TextField;
		public var Arrow:Sprite;
		private var doLoader:Loader;
		private var link:String;
		private var target:String;
		private var flag:Boolean;
		private var totalItems:Number;		
		private var subMenuHolder:Sprite;
		private var lastY:Number;
		private var imagePadding:Number;
		
		//constructor
		public function MenuItem() {
			this.buttonMode = true;
			Title.mouseEnabled = false;
			subTitle.mouseEnabled = false;
			this.addEventListener(MouseEvent.ROLL_OVER, onRollOverHandler);
			this.addEventListener(MouseEvent.ROLL_OUT, onRollOutHandler);
			overState.alpha = 0;
			hitState.alpha = 0;
			subTitle.autoSize = "left";
		}
		
		//handler for the roll over event
		private function onRollOverHandler(e:MouseEvent):void {
			Tweener.addTween(overState, { alpha:1, time: 0.3, transition: "easeoutquad" } );
			Tweener.addTween(Arrow, { _color:0x8a8994, time: 0.3, transition: "easeoutquad" });
		}
		
		//handler for the roll out event
		private function onRollOutHandler(e:MouseEvent):void {
			Tweener.addTween(overState, { alpha:0, time: 0.3, transition: "easeoutquad" } );
			Tweener.addTween(Arrow, { _color:0x9F9DAB,  time: 0.3, transition: "easeoutquad" });
		}
		
		//resize function
		public function resize(w:Number, h:Number):void {
			outState.width = w;
			outState.height = h;
			overState.width = w;
			overState.height = h;
			hitState.width = w;
			hitState.height = h;	
		}
		public function setImageAndTextOld(a:XML,w:Number,h:Number,padding:Number, tw:Number, th:Number, dp:DataProvider):void {
			var titleText:String = a.label.text();
			setImageAndText(a,w,h,padding, tw, th, dp, titleText);
		}
		//set the thumb image
		public function setImageAndText(a:XML,w:Number,h:Number,padding:Number, tw:Number, th:Number, dp:DataProvider, titleText:String):void {
			imagePadding = padding;
			var path:String = a.thumbnail.text();
			var descText:String = a.description.text();
			if (path>"") {
				var bigThumb:BigThumb = BigThumb(addChild(new BigThumb));
				bigThumb.x = padding;
				bigThumb.y = padding;
				bigThumb.setImage(path);
				bigThumb.resize(w, h);
				Title.x = bigThumb.x + bigThumb.width + 7;
				Title.y = 6;
				Title.width = outState.width - bigThumb.width - 3 * padding -20 ;
				Title.htmlText = titleText;
				subTitle.x = Title.x;
				subTitle.width = Title.width;
				
				subTitle.htmlText = descText;
				if (Math.round(subTitle.height) + Title.height + 9 > outState.height) {
					bigThumb.y = Title.y + 5;
				}
				outState.height = Math.round(subTitle.height) + Title.height + 9;
				
				overState.height = outState.height;
				hitState.height = outState.height;
			} else {
				Title.x =  6;
				Title.htmlText = titleText;
				//Title.text = titleText;
				subTitle.x = Title.x;
				subTitle.htmlText = descText;
				Title.width = outState.width - 3;// * padding -20 ;
				//Title.htmlText = titleText;
				subTitle.x = Title.x;
				subTitle.width = Title.width;
				
				subTitle.htmlText = descText;
				outState.height = Math.round(subTitle.height) + Title.height + 7;
				overState.height = outState.height;
				hitState.height = outState.height;
				
			}
			if (descText == "") {
				Title.y = outState.height / 2 - Title.height / 2-1;
			}
			if (a.subMenu.entry.length() == 0) {
				Arrow.visible = false;
				this.addEventListener(MouseEvent.CLICK, urlClick);
				link = a.subMenu.link.text();
				target = a.subMenu.target.text();
				
			} else {
				this.addEventListener(MouseEvent.CLICK, menuClick);
				buildSubMenu(a,tw,th,dp,titleText);
			}
			Arrow.y = outState.height / 2;
			Arrow.x = outState.width - 15;
		}
		
		public function setImageAndTextBackground(a:XML,w:Number,h:Number,padding:Number, tw:Number, th:Number, dp:DataProvider):void {
			imagePadding = padding;
			var path:String = a.thumbnail.text();
			var descText:String = a.description.text();
			
			Title.x =  6;
			Title.htmlText = "Background";
			subTitle.x = Title.x;
			subTitle.htmlText = descText;
			Title.width = (outState.width - 3) * padding * 4;// -20 ;
			subTitle.x = Title.x;
			subTitle.width = Title.width;
			
			subTitle.htmlText = descText;
			outState.height = Math.round(subTitle.height) + Title.height + 7;
			overState.height = outState.height;
			hitState.height = outState.height;
			
			if (descText == "") {
				Title.y = outState.height / 2 - Title.height / 2-1;
			}
			if (a.subMenu.entry.length() == 0) {
				Arrow.visible = false;
				this.addEventListener(MouseEvent.CLICK, urlClick);
				link = a.subMenu.link.text();
				target = a.subMenu.target.text();
				
			} else {
				this.addEventListener(MouseEvent.CLICK, menuClick);
				buildSubMenu(a,tw,th,dp,"Background");
			}
			Arrow.y = outState.height / 2;
			Arrow.x = outState.width - 15;
		}
		
		public function setImageAndTextShape(a:XML,w:Number,h:Number,padding:Number, tw:Number, th:Number, dp:DataProvider):void {
			imagePadding = padding;
			var path:String = a.thumbnail.text();
			var descText:String = a.description.text();
			
			Title.x =  6;
			Title.htmlText = "Shape";
			subTitle.x = Title.x;
			subTitle.htmlText = descText;
			Title.width = outState.width - 3;// * padding -20 ;
			subTitle.x = Title.x;
			subTitle.width = Title.width;
			
			subTitle.htmlText = descText;
			outState.height = Math.round(subTitle.height) + Title.height + 7;
			overState.height = outState.height;
			hitState.height = outState.height;
			
			if (descText == "") {
				Title.y = outState.height / 2 - Title.height / 2-1;
			}
			if (a.subMenu.entry.length() == 0) {
				Arrow.visible = false;
				this.addEventListener(MouseEvent.CLICK, urlClick);
				link = a.subMenu.link.text();
				target = a.subMenu.target.text();
				
			} else {
				this.addEventListener(MouseEvent.CLICK, menuClick);
				buildSubMenu(a,tw,th,dp,"Shape");
			}
			Arrow.y = outState.height / 2;
			Arrow.x = outState.width - 15;
		}
		public function setImageAndTextUpload(a:XML,w:Number,h:Number,padding:Number, tw:Number, th:Number, dp:DataProvider):void {
			imagePadding = padding;
			var path:String = a.thumbnail.text();
			var descText:String = a.description.text();
			
			Title.x =  6;
			Title.htmlText = "Upload";
			subTitle.x = Title.x;
			subTitle.htmlText = descText;
			Title.width = outState.width - 3;// * padding -20 ;
			subTitle.x = Title.x;
			subTitle.width = Title.width;
			
			subTitle.htmlText = descText;
			outState.height = Math.round(subTitle.height) + Title.height + 7;
			overState.height = outState.height;
			hitState.height = outState.height;
			
			if (descText == "") {
				Title.y = outState.height / 2 - Title.height / 2-1;
			}
			if (a.subMenu.entry.length() == 0) {
				Arrow.visible = false;
				this.addEventListener(MouseEvent.CLICK, urlClick);
				link = a.subMenu.link.text();
				target = a.subMenu.target.text();
				
			} else {
				this.addEventListener(MouseEvent.CLICK, menuClick);
				buildSubMenu(a,tw,th,dp,"Upload");
			}
			Arrow.y = outState.height / 2;
			Arrow.x = outState.width - 15;
		}
		//build sub menu
		private function buildSubMenu(a:XML,tw:Number,th:Number,dp:DataProvider, titleText:String):void
		{
			totalItems = a.subMenu.entry.length();
			subMenuHolder = new Sprite();
			addChild(subMenuHolder);
			lastY= outState.height;
			for (var i:int = 0; i < totalItems; i++) {
				var subItem:SubMenuItem = SubMenuItem(subMenuHolder.addChild(new SubMenuItem())); 
				subItem.x = 2;
				subItem.y = lastY;
				subItem.setImageAndText(a.subMenu.entry[i],imagePadding,tw,th,outState.width,430,dp,titleText);
				lastY += subItem.height;				
			}
			//Main.addHeight(lastY);
			
		}
		
		override public function set alpha(value:Number):void { }
		override public function get alpha():Number 
		{ 
			return lastY;
		}
		
		//expand the menu when a menu item is clicked
		private function menuClick(e:MouseEvent):void 
		{
			
			if (!flag)
			{
			Tweener.addTween(Arrow, { rotation: -90, time: 0.3, transition: "easeoutquad" } );
			showHitState();
			flag = true;
			}
			else
			{
				Tweener.addTween(Arrow, { rotation: 0, time: 0.3, transition: "easeoutquad" } );
				hideHitState();
				flag = false;
			}
			
		}
		
		public function showHitState():void
		{
			//trace("hit");
			Tweener.addTween(hitState, { alpha:1, time: 0.3, transition: "easeoutquad" } );
		}
		
		public  function hideHitState():void
		{
			Tweener.addTween(hitState, { alpha:0, time: 0.3, transition: "easeoutquad" } );
		}
		
		public function disableClick():void
		{
			this.mouseEnabled = false;
			this.buttonMode = false;
			this.removeEventListener(MouseEvent.CLICK, urlClick);
		}
		
		//reset arrow position
		public function resetArrow():void
		{
			//trace("11");
			Tweener.addTween(Arrow, { rotation: 0, time: 0.3, transition: "easeoutquad" } );
			hideHitState();
			flag = false;
		}
		
		//handler for the item click event
		private function urlClick(e:MouseEvent):void 
		{
			navigateToURL(new URLRequest(link), target || "_blank");
		}
		
		
		
		//get&set height
		override public function set height(value:Number):void { }
		override public function get height():Number 
		{ 
			return outState.height;
		}
		
		
	}
}